package org.yaukie.support.base;

import java.io.File;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaukie.util.ClassUtil;

public abstract class BaseClass {
    private static final Logger log = LoggerFactory.getLogger(BaseClass.class);
    protected final String packageName;

    protected BaseClass(String packageName) {
        this.packageName = packageName;
    }

    public final List<Class<?>> getClassList() {
        List<Class<?>> List = new ArrayList();

        try {
            Enumeration<URL> urls = ClassUtil.getClassLoader().getResources(this.packageName.replaceAll("\\.", "/"));

            while(true) {
                while(true) {
                    URL url;
                    do {
                        if (!urls.hasMoreElements()) {
                            return List;
                        }

                        url = (URL)urls.nextElement();
                    } while(url == null);

                    String protocol = url.getProtocol();
                    if (protocol.equals("file")) {
                        String packagePath = url.getPath().replaceAll("%20", " ");
                        this.addClass(List, packagePath, this.packageName);
                    } else {
                        JarURLConnection jarUrl = (JarURLConnection)url.openConnection();
                        if (jarUrl != null) {
                            JarFile jarFile = jarUrl.getJarFile();
                            Enumeration<JarEntry> jarEntries = jarFile.entries();

                            while(jarEntries.hasMoreElements()) {
                                JarEntry je = (JarEntry)jarEntries.nextElement();
                                String jarEntryName = je.getName();
                                if (jarEntryName.endsWith(".class")) {
                                    String className = jarEntryName.substring(0, jarEntryName.lastIndexOf(".")).replaceAll("/", ".");
                                    this.doAddClass(List, className);
                                }
                            }
                        }
                    }
                }
            }
        } catch (IOException var11) {
            if (log.isErrorEnabled()) {
                log.error("get class List failure ....", var11);
                throw new RuntimeException(var11);
            } else {
                return List;
            }
        }
    }

    private void addClass(List<Class<?>> classList, String packagePath, String packageName) {
        File[] files = ClassUtil.getClassFiles(packagePath);
        File[] var5 = files;
        int var6 = files.length;

        for(int var7 = 0; var7 < var6; ++var7) {
            File file = var5[var7];
            String fileName = file.getName();
            String className;
            if (file.isFile()) {
                className = ClassUtil.getClassName(packageName, fileName);
                this.doAddClass(classList, className);
            } else {
                className = ClassUtil.getSubPackagePath(packagePath, fileName);
                String subPackageName = ClassUtil.getSubPackageName(packageName, fileName);
                this.addClass(classList, className, subPackageName);
            }
        }

    }

    private void doAddClass(List<Class<?>> classList, String className) {
        Class<?> cls = ClassUtil.loadClass(className, false);
        if (this.checkAddClass(cls)) {
            classList.add(cls);
        }

    }

    public abstract boolean checkAddClass(Class<?> var1);
}
